/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package view.GUI;

import java.util.HashMap;
import java.util.Map;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

/**
 *
 * @author Ben
 */
public class LabelReader {
    private static volatile LabelReader reader;
    private ResourceBundle bundle;
    private Map<String, String> extraBundle;

    public static String noTemplate(String name){
        //remove template parameters like infoStore<Integer> will become infoStore
        int firstSign = name.indexOf('<');
        int secondSign = name.indexOf('>');
        if(firstSign < secondSign){
            name = name.substring(0,firstSign);
        }
        return name;
    }

    public String getString(String name) throws MissingResourceException{
        String value = null;
        try{
            value = bundle.getString(noTemplate(name));
        }catch(MissingResourceException ex){
            value = extraBundle.get(name);
            if(value == null){
                value = "";
            }
        }
        return value;
    }

    public static String toHTML(String source){
        return  "<html>" + source.replace("\n","<br>") + "</html>";
    }

    public String getHTMLString(String name){
         return toHTML(getString(name));
    }

    private LabelReader(){
        bundle = ResourceBundle.getBundle( "view.GUI.Labels");
        extraBundle = new HashMap<String,String>();
    }

    public void addString(String key, String value){
        extraBundle.put(key, value);
    }

    public static LabelReader getInstance() {
        if (reader == null){
            synchronized(LabelReader.class){
                if (reader == null){
                    reader = new LabelReader();
                }
            }
        }
        return reader;
    }

}
